package com.swq.java.code.chapter11.code08;

import java.io.Serializable;
import java.util.*;

/**
 * 演示枚举类
 */
public class Gender implements Serializable {
    private final Character sex;
    private final transient String description;

    public Character getSex() {
        return sex;
    }

    public String getDescription() {
        return description;
    }

    private static final Map<Character, Gender> instancesBySex =
            new HashMap<Character, Gender>();

    /**
     * 把构造方法声明为private类型，以便禁止外部程序创建Gender类的实例
     */
    private Gender(Character sex, String description) {
        this.sex = sex;
        this.description = description;
        instancesBySex.put(sex, this);
    }

    public static final Gender FEMALE =
            new Gender(new Character('F'), "Female");

    public static final Gender MALE =
            new Gender(new Character('M'), "Male");

    public static Collection getAllValues() {
        return Collections.unmodifiableCollection(instancesBySex.values());
    }

    /**
     * 按照参数指定的性别缩写查找Gender实例
     */
    public static Gender getInstance(Character sex) {
        Gender result = (Gender) instancesBySex.get(sex);
        if (result == null) {
            throw new NoSuchElementException(sex.toString());
        }
        return result;
    }

    public String toString() {
        return description;
    }

    /**
     * 保证反序列化时直接返回Gender类包含的静态实例
     */
    private Object readResolve() {
        return getInstance(sex);
    }

    public static void main(String args[]) {
        Gender female = Gender.getInstance(new Character('F'));
        System.out.println(female);
        Gender male = Gender.getInstance(new Character('M'));
        System.out.println(male);
    }
}